var utils = {
  sleep_and_log(duration) {
    var interval = 5;
    while (duration > interval) {
      sleep(interval * 1000);
      duration = duration - interval;
      console.log("还剩:" + duration + "s");
    }
    if (duration > 0) {
      sleep(duration * 1000);
      console.log("sleep_and_log: duration ", duration, "s");
    }
  },
  click_text(content) {
    if (this.exists_text(content)) {
      var uiText = text(content).findOne();
      var bounds = uiText.bounds();
      click(bounds.centerX(), bounds.centerY());
      return uiText;
    } else {
      return false;
    }
  },
  click_text_starts_with(content) {
    if (this.exists_text_starts_with(content)) {
      var uiText = textStartsWith(content).findOne();
      var bounds = uiText.bounds();
      click(bounds.centerX(), bounds.centerY());
      return uiText;
    } else {
      return false;
    }
  },
  exists_text(content) {
    if (text(content).exists()) {
      return text(content).findOne();
    } else {
      return false;
    }
  },
  exists_text_starts_with(content) {
    if (textStartsWith(content).exists()) {
      return textStartsWith(content).findOne();
    } else {
      return false;
    }
  },
  get_center_point(x1, y1, x2, y2) {
    return {
      x: x1 + (x2 - x1) / 2,
      y: y1 + (y2 - y1) / 2,
    };
  },
  click_point(x1, y1, x2, y2) {
    var point = this.get_center_point(x1, y1, x2, y2);
    return click(point.x, point.y);
  },
  click_bounds(bounds) {
    click(bounds.centerX(), bounds.centerY());
  },
  click_ui(ui_item) {
    while (true) {
      if (ui_item && ui_item.clickable()) {
        ui_item.click();
        return true;
      } else if (ui_item) {
        ui_item = ui_item.parent();
      } else {
        return false;
      }
    }
  },
  click_x_y(x, y) {
    click(x, y);
  },
  swipe_up(duration, distance) {
    while (duration > 3) {
      swipe(
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 2 - distance,
        500 + random(0, 10)
      );
      var interval = 1 + random(0, 2);
      sleep(interval * 1000);
      duration = duration - interval;
    }
    if (duration > 0) {
      sleep(duration * 1000);
    }
    utils.toast("swipe_up end");
  },
  toast(content) {
    console.log(content);
    toast(content);
    sleep(6 * 1000);
  },
  sleep(duration) {
    var duration = duration / 1000;
    while (duration > 4) {
      sleep(4 * 1000);
      duration = duration - 4;
    }
    if (duration > 0) {
      sleep(duration * 1000);
    }
  },
  get_gold(content) {
    var reg_exp = /(\d+)/g;
    var gold_match = content.match(reg_exp);
    var gold = parseInt(gold_match ? gold_match[0] : 0);
    return gold;
  },
  get_size_point(points) {
    var length = points.length;
    var size_point = [];
    var rate_x = device.width / 720;
    var rate_y = device.height / 1650;
    for (var i = 0; i < length; i++) {
      var x_y = points[i];
      var rate = i % 2 == 0 ? rate_x : rate_y;
      size_point.push(parseInt(x_y * rate));
    }
    return size_point;
  },
};

var constant = {
  min_ad_time: 40, //最小广告时间
  min_ad_swipe_time: 10, //最小广告【查看详情】滑动时间
  min_ad_after_time: 5, //判断下一个广告前的等待时间
  min_click_after_time: 2, //页面点击之后，进行下一步操作之前的等待时间
  min_gold: 0, //最小金币数量
  read_start_index: 5, //首页阅读，从第几个开始
  read_times: 2, //阅读一篇文章的下拉次数
  video_times: 20, //看视频总的下拉次数
};

var point_map = {
  首页视频: [147, 1534, 278, 1618],
  首页百度: [16, 1534, 147, 1618],
  首页任务: [278, 1534, 441, 1618],

  任务页的关闭: [882, 509, 970, 597],

  开宝箱之后的关闭: [550, 288, 606, 344],
  开福袋之后的关闭: [632, 344, 686, 400],
  看广告赚钱: [52, 910, 202, 942],
};



var ad_util = {
  sleep_and_log(duration) {
    var interval = 5;
    while (duration > interval) {
      if (utils.exists_text_starts_with("已领取")) {
        var after_interval = random(3, 6);
        console.log(
          "找到【已领取】,再过后" + after_interval + "s后停止当前广告"
        );
        sleep(after_interval * 1000);
        return;
      }
      if (utils.exists_text_starts_with("已完成任务")) {
        var after_interval = random(3, 6);
        console.log(
          "找到【已完成任务】,再过后" + after_interval + "s后停止当前广告"
        );
        sleep(after_interval * 1000);
        return;
      }
      sleep(interval * 1000);
      duration = duration - interval;
      console.log("还剩:" + duration + "s");
    }
    if (duration > 0) {
      sleep(duration * 1000);
      console.log("sleep_and_log: duration ", duration, "s");
    }
  },
  run() {
    var ad_interval = constant.min_ad_time + random(1, 3);
    this.sleep_and_log(ad_interval);
    console.log("ad_util execute: end");
    this.go_back();
    this.after();
  },
  execute() {
    console.show();
    sleep(1 * 1000);
    this.run();
  },
  go_back() {
    var index = 0;
    while (true) {
      if (index > 3) {
        return;
      }
      back();
      sleep(constant.min_click_after_time * 1000);
      if (utils.exists_text_starts_with("跳过")) {
        return;
      }
      if (utils.exists_text_starts_with("立即收下")) {
        return;
      }
      if (utils.exists_text_starts_with("开心收下")) {
        return;
      }
      if (utils.exists_text_starts_with("看视频再得")) {
        return;
      }
      if (utils.exists_text_starts_with("奖励已领取")) {
        return;
      }
      if (utils.exists_text_starts_with("领取成功")) {
        return;
      }
      if (utils.exists_text_starts_with("再看1个获得")) {
        return;
      }
      if (
        utils.exists_text_starts_with("百度") &&
        utils.exists_text_starts_with("视频") &&
        utils.exists_text_starts_with("小说")
      ) {
        console.log("go_back 找到【百度】【视频】【小说】");
        return;
      }
      if (utils.exists_text_starts_with("面对面")) {
        console.log("go_back 找到【面对面】");
        return;
      }
      if (utils.exists_text_starts_with("拒绝")) {
        utils.click_text("拒绝");
        console.log("go_back 找到【拒绝】");
        sleep(constant.min_click_after_time * 1000);
        back();
        sleep(constant.min_click_after_time * 1000);
      }
      index++;
      console.log("go_back index: ", index);
    }
  },
  after() {
    sleep(constant.min_ad_after_time * 1000);
    var ui_text_1 = utils.exists_text_starts_with("看视频再得");
    if (ui_text_1) {
      console.log("gold_video after: 找到【看视频再得】");
      utils.click_text_starts_with("看视频再得");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_2 = utils.exists_text_starts_with("再看一个领取");
    if (ui_text_2) {
      console.log("ad_util after: 找到【再看一个领取】" + ui_text_2.text());
      var gold = utils.get_gold(ui_text_2.text());
      console.log("ad_util after: " + gold + "金币");
      utils.click_text_starts_with("再看一个领取");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    var ui_text_3 = utils.exists_text_starts_with("再看1个获得");
    if (ui_text_3) {
      console.log("ad_util after: 找到【再看1个获得】");
      utils.click_text_starts_with("再看1个获得");
      sleep(constant.min_click_after_time * 1000);
      this.run();
      return;
    }
    console.log(
      "ad_util after: 没找到【再看一个领取】或【再看1个获得】或【看视频再得】"
    );

    if (utils.exists_text_starts_with("跳过")) {
      utils.click_text_starts_with("跳过");
      sleep(constant.min_click_after_time * 1000);
    }
    this.next();
  },
  next() {
    console.hide();
    sleep(1 * 1000);
    console.log("ad_util next");
  },
};

//任务 看广告赚钱
var DELTA_X = 598 - 127;
var DELTA_Y = 948 - 926;
function click_ad() {
  console.log("click_ad");
  var ui_item = utils.exists_text("看广告赚钱");
  var ui_item_bounds = ui_item.bounds();
  var rate_x = device.width / 720;
  var rate_y = device.height / 1650;
  var x = ui_item_bounds.centerX() + DELTA_X * rate_x;
  var y = ui_item_bounds.centerY() + DELTA_Y * rate_y;
  utils.click_x_y(parseInt(x), parseInt(y));
}

var ad = {
  go_page_from_home() {
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("视频") &&
      !!utils.exists_text("小说")
    ) {
      console.log("ad:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("ad:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    console.log("ad start");
    this.init();
  },
  init() {
    var ui_text =
      utils.exists_text_starts_with("看视频领取") ||
      utils.exists_text_starts_with("看视频再领") ||
      utils.exists_text_starts_with("看视频额外再领") ||
      utils.exists_text_starts_with("再看一条领");
    if (ui_text) {
      console.log(
        "ad init: 找到【看视频领取】或【看视频额外再领】或【再看一条领】或【看视频再领】" +
          ui_text.text()
      );
      var gold = utils.get_gold(ui_text.text());
      console.log("ad init: " + gold + "金币");
      if (gold >= constant.min_gold) {
        if (utils.click_text_starts_with("看视频领取")) {
        }
        if (utils.click_text_starts_with("看视频再领")) {
        }
        if (utils.click_text_starts_with("看视频额外再领")) {
        }
        if (utils.click_text_starts_with("再看一条领")) {
        }
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
      } else {
        sleep(constant.min_click_after_time * 1000);
        this.next();
      }
    } else {
      console.log(
        "ad init: 没找到【看视频领取】或【看视频再领】或【看视频额外再领】或【再看一条领】"
      );
      this.next();
    }
  },
  next() {
    console.log("ad next");
    if (utils.click_text("立即收下")) {
      console.log("ad next: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (utils.click_text("开心收下")) {
      console.log("ad next: 找到【开心收下】");
      sleep(constant.min_click_after_time * 1000);
    }
  },
};

//我的 -> 邀请好友 -> 开福袋
var bless = {
  go_page_from_home() {},
  start() {
    var ui_text = utils.click_text("开福袋");
    if (ui_text) {
      console.log("bless start: 找到【开福袋】");
      sleep(constant.min_click_after_time * 1000);
      this.init();
    } else {
      console.log("bless start: 没找到【开福袋】");
    }
  },
  init() {
    var ui_text =
      utils.exists_text_starts_with("再看一个领取") ||
      utils.exists_text_starts_with("观看视频再得") ||
      utils.exists_text_starts_with("看广告再") ||
      utils.exists_text_starts_with("看视频再领");
    if (ui_text) {
      console.log(
        "bless init: 找到【再看一个领取】或【观看视频再得】或【看广告再】或【看视频再领】" +
          ui_text.text()
      );
      var gold = utils.get_gold(ui_text.text());
      console.log("bless init: " + gold + "金币");
      if (gold > constant.min_gold) {
        if (utils.click_text_starts_with("再看一个领取")) {
        }
        if (utils.click_text_starts_with("观看视频再得")) {
        }
        if (utils.click_text_starts_with("看广告再")) {
        }
        if (utils.click_text_starts_with("看视频再领")) {
        }
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
      } else {
        utils.click_point.apply(
          utils,
          utils.get_size_point(point_map["开福袋之后的关闭"])
        );
        sleep(constant.min_click_after_time * 1000);
        this.next();
      }
    } else {
      console.log(
        "bless init: 没找到【再看一个领取】或【观看视频再得】或【看广告再】"
      );
      this.next();
    }
  },
  next() {
    console.log("bless next");
    if (utils.exists_text_starts_with("看广告再")) {
      console.log("bless next: 找到【看广告再】");
      this.init();
      return;
    }
    if (utils.click_text("开心收下")) {
      console.log("bless next: 找到【开心收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (utils.click_text("立即收下")) {
      console.log("bless next: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
  },
};

//金币 看视频赚钱
var gold_video = {
  go_page_from_home() {
    this.start();
  },
  start() {
    console.log("gold_video start");
    this.init();
  },
  init() {
    var ui_text = utils.exists_text_starts_with("看视频赚");
    if (ui_text) {
      console.log("gold_video init: 找到【看视频赚】" + ui_text.text());
      var gold = utils.get_gold(ui_text.text());
      console.log("gold_video init: " + gold + "金币");
      if (gold > constant.min_gold) {
        utils.click_text_starts_with("看视频赚");
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
      } else {
        console.log("gold_video init: 广告金币太少");
        sleep(constant.min_click_after_time * 1000);
        this.next();
      }
    } else {
      console.log("gold_video init: 没找到【看视频赚】");
      this.next();
    }
  },
  next() {
    console.log("gold_video next");
    if (utils.exists_text_starts_with("看广告再")) {
      console.log("gold_video next: 找到【看广告再】");
      this.init();
      return;
    }
    if (utils.click_text("开心收下")) {
      console.log("gold_video next: 找到【开心收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (utils.click_text("立即收下")) {
      console.log("gold_video next: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
  },
};

//阅读领取奖励金币之后，看广告再领
var read_ad = {
  go_page_from_home() {},
  start() {
    console.log("read_ad start");
    sleep(2 * 1000);
    this.init();
  },
  init() {
    var ui_text = utils.exists_text_starts_with("看广告再领");
    if (ui_text) {
      console.log("read_ad init: 找到【看广告再领】");
      var gold = utils.get_gold(ui_text.text());
      console.log("read_ad init: " + gold + "金币");
      if (gold > constant.min_gold) {
        utils.click_text_starts_with("看广告再领");
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
      } else {
        console.log("read_ad init: 广告金币太少");
        sleep(constant.min_click_after_time * 1000);
        this.next();
      }
    } else {
      console.log("read_ad init: 没找到【看广告再领】");
      this.next();
    }
  },
  next() {
    console.log("read_ad next");
    if (utils.exists_text_starts_with("看广告再")) {
      console.log("read_ad next: 找到【看广告再】");
      this.init();
      return;
    }
    if (utils.click_text("开心收下")) {
      console.log("read_ad next: 找到【开心收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (utils.click_text("立即收下")) {
      console.log("read_ad next: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
  },
};

var read = {
  items: [],
  index: 0,
  go_page_from_home() {
    this.items = [];
    this.index = 0;
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("视频") &&
      !!utils.exists_text("小说")
    ) {
      console.log("read:go_page_from_home");
      utils.click_text("百度");
      sleep(constant.min_click_after_time * 1000);
      utils.click_text("推荐");
      sleep(constant.min_click_after_time * 1000);
      utils.click_text("推荐");
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("read:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    this.items = id("amx").find();
    if (this.items.length > 0) {
      console.log("read start: 找到【id('amx')】 " + this.items.length);
      this.init();
    } else {
      console.log("read start: 没找到【id('amx')】");
      return;
    }
  },
  init() {
    var item = this.items[constant.read_start_index + this.index];
    sleep(constant.min_click_after_time * 1000);
    if (utils.click_ui(item)) {
      console.log("read init: " + this.index + " 点击成功");
      sleep(constant.min_click_after_time * 1000);
      this.execute(constant.read_times);
    } else {
      console.log("read init: " + this.index + " 点击失败");
      this.next();
    }
  },
  execute() {
    var times = constant.read_times;
    var swipe_interval = 400 + random(10, 20);
    while (times > 0) {
      swipe(
        device.width / 2,
        (device.height / 10) * 9,
        device.width / 2,
        (device.height / 10) * 2,
        swipe_interval
      );
      var interval = 1 + random(1, 2);
      sleep(interval * 1000);
      times = times - 1;
    }
    console.log("read execute: end");
    this.after();
  },
  after() {
    console.log("read after");
    back();
    this.next();
  },
  next() {
    this.index = this.index + 1;
    if (this.index > 1) {
      return;
    } else {
      this.init();
    }
  },
};

var treasure = {
  go_page_from_home() {
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("视频") &&
      !!utils.exists_text("小说")
    ) {
      console.log("treasure:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("treasure:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    var ui_text =
      utils.exists_text_starts_with("再看一个领取") ||
      utils.exists_text_starts_with("观看视频再得") ||
      utils.exists_text_starts_with("看广告再");
    if (ui_text) {
      console.log(
        "treasure start: 找到【再看一个领取】或【观看视频再得】或【看广告再】" +
          ui_text.text()
      );
      var gold = utils.get_gold(ui_text.text());
      if (gold > constant.min_gold) {
        if (utils.click_text_starts_with("再看一个领取")) {
        }
        if (utils.click_text_starts_with("观看视频再得")) {
        }
        if (utils.click_text_starts_with("看广告再")) {
        }
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
        return;
      } else {
        utils.click_point.apply(
          utils,
          utils.get_size_point(point_map["进入任务之后的关闭"])
        );
        sleep(constant.min_click_after_time * 1000);
      }
    }
    if (utils.click_text("立即收下")) {
      console.log("treasure start: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    var ui_text = utils.click_text("开宝箱得金币");
    if (ui_text) {
      console.log("treasure start: 找到【开宝箱得金币】");
      sleep(constant.min_click_after_time * 1000);
      this.init();
    } else {
      console.log("treasure start: 没找到【开宝箱得金币】");
    }
  },
  init() {
    var ui_text =
      utils.exists_text_starts_with("再看一个领取") ||
      utils.exists_text_starts_with("观看视频再得") ||
      utils.exists_text_starts_with("看广告再");
    if (ui_text) {
      console.log(
        "treasure init: 找到【再看一个领取】或【观看视频再得】或【看广告再】" +
          ui_text.text()
      );
      var gold = utils.get_gold(ui_text.text());
      console.log("treasure init: " + gold + "金币");
      if (gold > constant.min_gold) {
        if (utils.click_text_starts_with("再看一个领取")) {
        }
        if (utils.click_text_starts_with("观看视频再得")) {
        }
        if (utils.click_text_starts_with("看广告再")) {
        }
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
      } else {
        utils.click_point.apply(
          utils,
          utils.get_size_point(point_map["开宝箱之后的关闭"])
        );
        sleep(constant.min_click_after_time * 1000);
        this.next();
      }
    } else {
      console.log(
        "treasure init: 没找到【再看一个领取】或【观看视频再得】或【看广告再】"
      );
      this.next();
    }
  },
  next() {
    console.log("treasure next");
    if (utils.exists_text_starts_with("看广告再")) {
      console.log("treasure next: 找到【看广告再】");
      this.init();
      return;
    }
    if (utils.click_text("开心收下")) {
      console.log("treasure next: 找到【开心收下】");
      sleep(2 * 1000);
    }
    if (utils.click_text("立即收下")) {
      console.log("treasure next: 找到【立即收下】");
      sleep(2 * 1000);
    }
  },
};

var video = {
  go_page_from_home() {
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("视频") &&
      !!utils.exists_text("小说")
    ) {
      console.log("video:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页视频"])
      );
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("video:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    console.log("video start");
    this.init();
  },
  init() {
    console.log("video init");
    this.execute();
  },
  execute() {
    var duration = constant.video_times;
    var swipe_interval = 500 + random(10, 20);
    while (duration > 0) {
      swipe(
        device.width / 2,
        (device.height / 10) * 9,
        device.width / 2,
        (device.height / 10) * 1,
        swipe_interval
      );
      var interval = 3 + random(1, 3);
      sleep(interval * 1000);

      var flag = random(1, 5);
      console.log(flag, duration);
      if (flag == 1) {
        if (utils.exists_text_starts_with("相关搜索")) {
          console.log(flag, "找到【相关搜索】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 935, 696, 1062])
          // );
        } else if (utils.exists_text_starts_with("下一条")) {
          console.log(flag, "找到【下一条】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 1189, 696, 1316])
          // );
        } else {
          utils.click_point.apply(
            utils,
            utils.get_size_point([624, 1005, 696, 1132])
          );
        }
        sleep(2 * 1000);
      }
      if (flag == 3) {
        if (utils.exists_text_starts_with("相关搜索")) {
          console.log(flag, "找到【相关搜索】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 1189, 696, 1316])
          // );
        } else if (utils.exists_text_starts_with("下一条")) {
          console.log(flag, "找到【下一条】");
          // utils.click_point.apply(
          //   utils,
          //   utils.get_size_point([624, 1189, 696, 1316])
          // );
        } else {
          utils.click_point.apply(
            utils,
            utils.get_size_point([624, 1259, 696, 1386])
          );
        }
        sleep(2 * 1000);
      }

      duration = duration - 1;
    }
    console.log("video execute");
    this.after();
  },
  after() {
    console.log("video after");
    this.next();
  },
  next() {
    console.log("video next");
  },
};

var novel = {
  go_page_from_home() {
    console.log("novel go_page_from_home");
    this.start();
  },
  start() {
    console.log("novel start");
    this.init();
  },
  init() {
    console.log("novel init");
    this.execute(10000);
  },
  execute(duration) {
    console.log("novel execute start");
    while (duration > 0) {
      swipe(
        (device.width / 10) * 9,
        device.height / 2,
        (device.width / 10) * 2,
        device.height / 2,
        500
      );
      sleep((5 + random(1, 3)) * 1000);
      if (utils.click_text("我知道了")) {
      }
      duration = duration - 1;
    }
    console.log("novel execute end");
    this.after();
  },
  after() {
    console.log("novel after");
    this.next();
  },
  next() {
    console.log("novel next");
  },
};

function main() {
      var index = 0;
      while (true) {
        if (index > 100) {
          return;
        }
        video.go_page_from_home();
        sleep(2 * 1000);
        treasure.go_page_from_home();
        sleep(2 * 1000);
        ad.go_page_from_home();
        sleep(2 * 1000);
        console.show();
        console.log("在等待下一个轮回,20s");
        sleep(20 * 1000);
        console.hide();
        sleep(2 * 1000);
        index = index + 1;
      }
    }
    main();